package com.bwie.bwedu.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.bwie.bwedu.R;
import com.bwie.bwedu.adapter.PersonalLvAdapter;
import com.bwie.bwedu.ui.MyListView;
import com.bwie.bwedu.ui.dialog.BounceTopEnter;
import com.bwie.bwedu.ui.dialog.NormalDialog;
import com.bwie.bwedu.ui.dialog.OnBtnClickL;
import com.bwie.bwedu.ui.dialog.SlideBottomExit;
import com.bwie.bwedu.ui.dialog.base.BaseAnimatorSet;
import com.bwie.bwedu.utils.ASimpleCacheUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/7/21.
 * 个人中心界面
 */
public class PersonalActivity extends BaseActivity {

    public List<String> list;
    private MyListView lv;
    private TextView username;
    private TextView title;
    private ImageButton back;
    private Button out_login;
    private BaseAnimatorSet bas_in;
    private BaseAnimatorSet bas_out;

    @Override
    public int getLayout() {
        return R.layout.activity_personal;
    }

    @Override
    public void findView() {
        back = (ImageButton) findViewById(R.id.item_title_back);
        title = (TextView) findViewById(R.id.item_title_title);
        lv = (MyListView) findViewById(R.id.personal_lv);
        username = (TextView) findViewById(R.id.personal_username);
        out_login = (Button) findViewById(R.id.out_login);
    }

    @Override
    public void setData() {
        initMenu();
        title.setText("个人主页");
        lv.setAdapter(new PersonalLvAdapter(this,list));
        String username = ASimpleCacheUtils.read("username");
        if (!TextUtils.isEmpty(username))
            this.username.setText(username);
    }

    @Override
    public void setListener() {
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(PersonalActivity.this, PersonalInfoUpdActivity.class);
                intent.putExtra("position",position+"");
                startActivity(intent);
                overridePendingTransition(R.anim.rightenter,R.anim.leftexit);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBack();
            }
        });
        out_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NormalDialogStyleTwo("退出登录后，会清除您的所有登录信息，您确定要退出登录么？");
            }
        });
    }

    public void initMenu(){
        list=new ArrayList<>();
        list.add("修改密码");
        list.add("消息设置");
    }

    private void onBack(){
        finish();
        overridePendingTransition(R.anim.rightenter,R.anim.leftexit);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK) {
           onBack();
           return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    /**
     * 显示对话框
     */
    private void NormalDialogStyleTwo(String str) {
        bas_in = new BounceTopEnter();
        bas_out = new SlideBottomExit();
        final NormalDialog dialog = new NormalDialog(this);
        dialog.content(str)//
                .style(NormalDialog.STYLE_TWO)//
                .titleTextSize(24)//
                .showAnim(bas_in)//
                .dismissAnim(bas_out)//
                .show();

        dialog.setOnBtnClickL(
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.dismiss();
                    }
                },
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        //退出登录====返回用户登录界面
                        startActivity(new Intent(PersonalActivity.this, LoginActivity.class));
                        //将用户登录信息清除
                        getSharedPreferences("isLogin", MODE_PRIVATE).edit()
                                .putString("username", "")
                                .putString("password", "")
                                .putString("token", "")
                                .commit();
                        finish();//关闭当前界面
                        dialog.dismiss();
                    }
                });

    }
}
